/*
 * 介绍 : tab首页
 * author :lvjing
 * time : 2017-6-15
 */
/**
 * 根据角色配置跳转的页面
 */
/**
 * resume 应用从后台切换到前台,重新初始化android
 */
var subpages = ["../home/home.html","../konwledge/homekone/fxzsk.html","../locat1/locat.html","../konwledge/hometwo/fxzsk.html","../mineMain/mineMain.html"];
var subpageIds = ["home.html","fxzsk1.html","locat1.html","fxzsk2.html","mineMain.html"];
//底部tab高度
var bottomHeight;
//标题高度
var topHeight;
//当前激活页面
var activeTab;
//子页面是否已显示过
var aniShow = {};
//标题栏id数组
//var headerIds = ["header_gdcx"];
//蒙版隐藏
window.addEventListener("closeMask", function() {
	mui(".mui-backdrop")[0].classList.add("mui-hidden");
});
//蒙版显示
window.addEventListener("showMask", function() {
	mui(".mui-backdrop")[0].classList.remove("mui-hidden");
});
var tabTaskFirst = true;
var self = null;
mui.init();
mui.plusReady(function() {
	initUI();
	initListener();
	rebuildBack();
});

/**
 * 初始化页面
 */
function initUI() {
	self = plus.webview.currentWebview();
	bottomHeight = mui("nav")[0].offsetHeight + "px";
	//topHeight = mui("header")[0].offsetHeight + "px";
	topHeight = 0 + "px";
	var subpageStyle = {
		top: topHeight,
		bottom: bottomHeight
	};
	//创建子页面，首个选项卡页面显示，其它均隐藏；
	var subpageLength = subpages.length;
	mui.each(subpages, function(i, item) {
		var temp = {};
		var sub;
		if(i==1){
			 sub = plus.webview.create(item, subpageIds[i], subpageStyle,{"type":"2001101"});
		}else if (i==3){
			 sub = plus.webview.create(item, subpageIds[i], subpageStyle,{"type":"2001102"});
		}else{
			 sub = plus.webview.create(item, subpageIds[i], subpageStyle);
		}
		if(i > 0) {
			sub.hide();
		} else {
			aniShow[i] = true;
			activeTab = subpageIds[0];
		}
		self.append(sub);
	})
}

/**
 * 初始化监听事件
 */
function initListener() {
	//选项卡点击事件
	mui('.mui-bar-tab').on('tap', '.mui-tab-item', function(e) {
		if(this.classList.contains("mui-active")) {
			return;
		}
		var index = this.getAttribute("index");
		/*mui("header").each(function() {
			this.classList.add("mui-hidden");
		});*/
		//		if(index == 1) {
		//			mui("#" + headerIds[0])[0].classList.remove("mui-hidden");
		//		}

		//显示目标选项卡
		//若为iOS平台或非首次显示，则直接显示
		if(mui.os.ios || aniShow[index]) {
			plus.webview.show(subpageIds[index]);
		} else {
			//否则，使用fade-in动画
			aniShow[index] = true;
			plus.webview.show(subpageIds[index], "none", 300);
		}
		//隐藏当前;
		plus.webview.hide(activeTab);
		//更改当前活跃的选项卡
		activeTab = subpageIds[index];
	});

}

/**
 * 重写返回事件，实现一秒内点两次返回才退出程序
 */
function rebuildBack() {
	var backButtonPress = 0;
	mui.back = function(event) {
		backButtonPress++;
		if(backButtonPress > 1) {
			plus.runtime.quit(); //退出应用
		} else {
			plus.nativeUI.toast('再按一次退出应用');
		}
		//点击返回一秒后将标志位nBackButtonPress清零
		setTimeout(function() {
			backButtonPress = 0;
		}, 1000);
		return false;
	};
}

/**
 * 判断字符串是否是数组的元素
 * @param {Object} str
 * @param {Object} array
 */
function stringInArray(str, array) {
	var isInArray = false;
	mui.each(array, function(i, item) {
		if(str == item) {
			isInArray = true;
		}
	})
	return isInArray;
}